<?php

function dbconnect() 
	{ 
		require('config.php'); 
		static $connect = null; 
		if ($connect === null) 
			{
				 $connect =  mysql_connect($host, $user, $passwd) or die(mysql_error());
				 mysql_query("SET character_set_results=utf8", $connect);
				 mysql_select_db($database); 
			} 
		return $connect; 
	} 
	
	function getSubcategory($parent){
        $sql = mysql_query("SELECT * FROM Chu_de WHERE Parent = ".$parent, dbconnect());   
        while($row = mysql_fetch_array($sql)){
            echo '<ul>';
            echo '<li><a href="#" >'.$row['Ten_chu_de'].'</a></li>';
            getSubcategory($row['ID']);         // *
            echo '</ul>';
        }   
    }
	
	function getbyid_parent($id)
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Chu_de WHERE Parent = ".$id,dbconnect());
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	
	function Menu($parent = 0, $space = " ", $trees = array())

			{
			if(!$trees) { $trees = array();}
			$sql = "SELECT * FROM Chu_de WHERE Parent = $parent";
			$query = mysql_query($sql, dbconnect());
			
			while($rs = mysql_fetch_assoc($query))
			
					{
					$trees[] = array( 'ID' => $rs['ID'],
										
										'Ten_chu_de'=>$space." ".$rs['Ten_chu_de'], 
										'Parent' => $rs['Parent'],);
			
					$trees = Menu($rs['ID'], $space."--", $trees); 
					
				    }
			return $trees;
			
			}
			
		function getid_auto_increment()
			{
		
		$r= mysql_query("SHOW TABLE STATUS LIKE 'Chu_de'",dbconnect());
		$row = mysql_fetch_array($r); 
        $n = $row['Auto_increment']; 
        return $n;
		
		} 	
	
	
	function insert_chude($cd) 
			{ 
		$query= mysql_query("INSERT INTO Chu_de SET Ten_chu_de=N'".$cd['Ten_chu_de']."', Parent=N'".$cd['Parent']."' ", dbconnect());  
			}

	function getbyid_chude($id)
	{
		$result= mysql_query("SELECT * FROM Chu_de WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	
	function update_chude($cd)
	{
	$query= mysql_query("UPDATE Chu_de SET Ten_chu_de=N'".$cd['Ten_chu_de']."', Parent=N'".$cd['Parent']."'  WHERE ID=N'".$cd['ID']."' ",dbconnect()); 	
	}
	function getbychude_sach($id)
	{ 
		$allnxb_req = mysql_query("SELECT * FROM sach WHERE ID_Chu_de = N'".$id."'" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	function getbychude_countsach($id)
	{ 
		$allnxb_req = mysql_query("SELECT count(*) FROM sach WHERE ID_Chu_de = N'".$id."'" ,dbconnect()); 
		return mysql_fetch_array($allnxb_req); 
	} 
	
?>